<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Crop an image automatically using one of the available modes</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.imagecrop.html">« imagecrop</a></li>
      <li style="float: right;"><a href="function.imagedashedline.html">imagedashedline »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.image.html">GD 和图像处理 函数</a></li>
    <li>Crop an image automatically using one of the available modes</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.imagecropauto" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">imagecropauto</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">imagecropauto</span> &mdash; <span class="dc-title">Crop an image automatically using one of the available modes</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.imagecropauto-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>imagecropauto</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="class.gdimage.html" class="type GdImage">GdImage</a></span> <code class="parameter">$image</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$mode</code><span class="initializer"> = <strong><code>IMG_CROP_DEFAULT</code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">float</span> <code class="parameter">$threshold</code><span class="initializer"> = 0.5</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$color</code><span class="initializer"> = -1</span></span><br>): <span class="type"><span class="type"><a href="class.gdimage.html" class="type GdImage">GdImage</a></span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   Automatically crops an image according to the given
   <code class="parameter">mode</code>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.imagecropauto-parameters">
  <h3 class="title">参数</h3>
  <dl>
   <dt>

<code class="parameter">image</code></dt>
<dd>
<p class="para">由图象创建函数(例如<span class="function"><a href="function.imagecreatetruecolor.html" class="function">imagecreatetruecolor()</a></span>)返回的 
<span class="classname"><a href="class.gdimage.html" class="classname">GdImage</a></span> 对象。</p></dd>

   
    <dt>
<code class="parameter">mode</code></dt>

    <dd>

     <p class="para">
      One of the following constants:
     </p>
     <dl>
      
       <dt>
<strong><code>IMG_CROP_DEFAULT</code></strong></dt>

       <dd>

        <span class="simpara">
         Same as <strong><code>IMG_CROP_TRANSPARENT</code></strong>.
         Before PHP 7.4.0, the bundled libgd fell back to <strong><code>IMG_CROP_SIDES</code></strong>,
         if the image had no transparent color.
        </span>
       </dd>

      
      
       <dt>
<strong><code>IMG_CROP_TRANSPARENT</code></strong></dt>

       <dd>

        <span class="simpara">
         Crops out a transparent background.
        </span>
       </dd>

      
      
       <dt>
<strong><code>IMG_CROP_BLACK</code></strong></dt>

       <dd>

        <span class="simpara">
         Crops out a black background.
        </span>
       </dd>

      
      
       <dt>
<strong><code>IMG_CROP_WHITE</code></strong></dt>

       <dd>

        <span class="simpara">
          Crops out a white background.
        </span>
       </dd>

      
      
       <dt>
<strong><code>IMG_CROP_SIDES</code></strong></dt>

       <dd>

        <span class="simpara">
         Uses the 4 corners of the image to attempt to detect the background to
         crop.
        </span>
       </dd>

      
      
       <dt>
<strong><code>IMG_CROP_THRESHOLD</code></strong></dt>

       <dd>

        <span class="simpara">
         Crops an image using the given <code class="parameter">threshold</code> and
         <code class="parameter">color</code>.
        </span>
       </dd>

      
     </dl>

    </dd>

   
   
    <dt>
<code class="parameter">threshold</code></dt>

    <dd>

     <p class="para">
      Specifies the tolerance in percent to be used while comparing the image
      color and the color to crop. The method used to calculate the color
      difference is based on the color distance in the RGB(a) cube.
     </p>
     <p class="para">
      Used only in <strong><code>IMG_CROP_THRESHOLD</code></strong> mode.
     </p>
     <blockquote class="note"><p><strong class="note">注意</strong>: 
      <span class="simpara">
       Before PHP 7.4.0, the bundled libgd used a somewhat different algorithm,
       so the same <code class="parameter">threshold</code> yielded different results
       for system and bundled libgd.
      </span>
     </p></blockquote>
    </dd>

   
   
    <dt>
<code class="parameter">color</code></dt>

    <dd>

     <p class="para">
      Either an RGB color value or a palette index.
     </p>
     <p class="para">
      Used only in <strong><code>IMG_CROP_THRESHOLD</code></strong> mode.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.imagecropauto-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a cropped image object on success 或者在失败时返回 <strong><code>false</code></strong>.
   If the complete image was cropped, <span class="function"><a href="function.imagecrop.html" class="function">imagecrop()</a></span> returns <strong><code>false</code></strong>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.imagecropauto-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
 <td>8.0.0</td>
 <td>
  <code class="parameter">image</code> expects a <span class="classname"><a href="class.gdimage.html" class="classname">GdImage</a></span>
  instance now; previously, a <span class="type">resource</span> was expected.
 </td>
</tr>

     <tr>
      <td>8.0.0</td>
      <td>
       On success, this function returns a <span class="classname"><a href="class.gdimage.html" class="classname">GDImage</a></span> instance now;
       previously, a <span class="type">resource</span> was returned.
      </td>
     </tr>

     <tr>
      <td>7.4.0</td>
      <td>
       The behavior of imagecropauto() in the bundled libgd has been synced with
       that of system libgd: <strong><code>IMG_CROP_DEFAULT</code></strong> no longer falls
       back to <strong><code>IMG_CROP_SIDES</code></strong> and threshold-cropping now uses
       the same algorithm as system libgd.
      </td>
     </tr>

     <tr>
      <td>7.4.0</td>
      <td>
       The default value of <code class="parameter">mode</code> has been changed to
       <strong><code>IMG_CROP_AUTO</code></strong>. Formerly, the default value has been
       <code class="literal">-1</code> which corresponds to <strong><code>IMG_CROP_DEFAULT</code></strong>,
       but passing <code class="literal">-1</code> is now deprecated.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.imagecropauto-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2662">
    <p><strong>示例 #1 Proper handling of auto-cropping</strong></p>
    <div class="example-contents"><p>
     As noted in the return value section, <span class="function"><strong>imagecropauto()</strong></span>
     returns <strong><code>false</code></strong> if the whole image
     was cropped. In this example we have an image object
     <code class="literal">$im</code> which should be automatically cropped only if there
     is something to crop; otherwise we want to proceed with the original image.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$cropped&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecropauto</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">IMG_CROP_DEFAULT</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$cropped&nbsp;</span><span style="color: #007700">!==&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{&nbsp;</span><span style="color: #FF8000">//&nbsp;in&nbsp;case&nbsp;a&nbsp;new&nbsp;image&nbsp;object&nbsp;was&nbsp;returned<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">imagedestroy</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;we&nbsp;destroy&nbsp;the&nbsp;original&nbsp;image<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$im&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$cropped</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;and&nbsp;assign&nbsp;the&nbsp;cropped&nbsp;image&nbsp;to&nbsp;$im<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.imagecropauto-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="function.imagecrop.html" class="function" rel="rdfs-seeAlso">imagecrop()</a> - Crop an image to the given rectangle</span></li>
  </ul>
 </div>


</div></div></div></body></html>